﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sob.Machine.WebPageParser.Demo;
using Sob.Common;
using System.Text.RegularExpressions;
using System.IO;
using Sob.Machine.WebPageParser.Entity;
using NLog;
using System.Diagnostics;
using System.Threading;

namespace Sob.Machine.WebPageParser
{
    class Program
    {
        private static Logger logger = LogManager.GetCurrentClassLogger();
        private static Stopwatch sw = new Stopwatch();
           

        static void Main(string[] args)
        {
            //1.
            //sw.Start();
            //TestCrawlPageHeader();
            //Console.Read();

            //2
            //TestGetCharset();
            //Console.Read();

            //3
            string url = "http://www.hc360.com/";//ISO-8859-1要转成gb2312
            //url = "http://www.cnblogs.com/";
            //url = "http://shop65062777.taobao.com/";
            //url = "http://www.sina.com.cn/";
            //url = "http://www.ifeng.com/";
            //url = "http://www.letao.com/";
            //url = "http://www.gome.com.cn/";
            //url = "http://www.coo8.com";
            //url = "http://www.yihaodian.com/";
            //url = "http://www.suning.com/";
            //url = "http://www.jiuxian.com/";//ISO-8859-1要转成utf-8
            url = "http://www.d1cm.com/";
            List<string> urls = new List<string>() { "http://www.hc360.com/", "http://www.ifeng.com/", "http://www.d1cm.com/", "http://www.jiuxian.com/", "http://www.gome.com.cn/" };

            foreach (string item in urls)
            {
                //if (!item.Equals("http://www.ifeng.com/")) continue;

                Crawler crawler = new Crawler(item);
                crawler.CrawlerProgress += new Crawler.CrawlerProgressEventHander(crawler_CrawlerProgress);

#if !DEBUG
                crawler.DownLoad();
#else
                Thread thread = new Thread(crawler.DownLoad);
                thread.Start();
                Thread.Sleep(100);
#endif
            }


            logger.Trace("URL:{0} is OK", url);
            Console.Read();
        }

        static void crawler_CrawlerProgress(CrawlInfo crawlInfo)
        {
            crawlInfo.useTime.Stop();

            logger.Error("访问主域<{0}>：（<Visited:{1}|Pending:{2}><Success:{3}|Failure:{4}>）；耗时：{5}",
                crawlInfo.BaseHost,
                crawlInfo.Visited, crawlInfo.Pending,
                crawlInfo.Success, crawlInfo.Failure,
                crawlInfo.useTime.Elapsed.ToString());

        }




        static void TestGetCharset()
        {
            List<string> htmls = new List<string>();
            htmls.Add("charset=\"utf-8\"");
            htmls.Add("charset='UTF-8'");
            htmls.Add("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            htmls.Add("<meta charset=utf-8 />");

            foreach (var html in htmls)
            {
                //Regex regex = new Regex("charset=(?<charset>[\\s\\S]*?)[\"|']", RegexOptions.IgnoreCase);
                //Match match = regex.Match(html);
                //if (match.Success)
                //{
                //    Console.WriteLine("----------:" + html);
                //    Console.WriteLine(match.Groups["charset"].Value);
                //}
                Console.WriteLine("\r\n测试....{0}", html);


                string pattern = "charset=(?<charset>[\\s\\S]*?)[\"|'|/>]";
                int startIndex = html.IndexOf("charset=", 0, html.Length > 200 ? 200 : html.Length);
                if (startIndex >= 0)
                {
                    startIndex += "charset=".Length;
                    string flagCharacter = html.Substring(startIndex, 1);
                    if (flagCharacter.Equals("'"))
                    {
                        pattern = "charset='(?<charset>[\\s\\S]*?)[\"|']";
                    }
                    else if (flagCharacter.Equals("\""))
                    {
                        pattern = "charset=\"(?<charset>[\\s\\S]*?)[\"|']";
                    }
                    Console.WriteLine("pattern：" + pattern);
                    Regex r = new Regex(pattern);
                    Match m = r.Match(html);
                    if (m.Success)
                    {
                        Console.WriteLine("结果是:" + m.Groups["charset"].Value);
                    }
                }

            }


        }

        static void TestCrawlPageHeader()
        {
           

            //CrawlPage cp = new CrawlPage("http://www.d1cm.com/");            
            //cp.DownloadPage();
            Crawler crawler = new Crawler("http://www.d1cm.com/");
            crawler.CrawlerProgress += new Crawler.CrawlerProgressEventHander(crawler_TestCrawlPageHeader);
            crawler.DownLoad();

            Console.Read();

        }

        static void crawler_TestCrawlPageHeader(CrawlInfo crawlInfo)
        {
            Console.WriteLine("{0}-", crawlInfo.Success);
            if (crawlInfo.Success == 100)
            {
                Console.WriteLine("耗时：{0}", crawlInfo.useTime.Elapsed.ToString());
                Console.WriteLine("耗时：{0}", sw.Elapsed.ToString());
                Console.Read();
            }
        }






    }
}
